package com.zkingsoft.actions.admin;

import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.EncrypUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.actions.common.BaseController;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.services.ClientService;

import org.springframework.stereotype.Controller;

import com.zkingsoft.model.Client;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-11-11 08:37
 */
@Controller
@RequestMapping(value = "admin/client")
public class ClientController extends BaseController{

	@Resource
	private ClientService currentService;
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Client client, PaginationVO pageVo) {
		return showList(currentService, client, pageVo);
	}
   	
	/**
	 * 新增或者修改页面
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Client client) {
		try {
			if(client.getPassword()!=null && !client.getPassword().equals("")){
			client.setPassword(EncrypUtil.getMD5(client.getPassword()));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (client.getId() != null) {
			return modify(currentService, client, "客户信息");
		} else {
			return add(currentService, client, "客户信息");
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		Client client;
		if (id != null) {
			client = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", client);
		}
		return "admin/client-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}
  
}